package org.dromara.web.service.business;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.dao.business.domain.UserBalanceLog;
import org.dromara.dao.business.domain.UserBalanceLogQuery;
import org.dromara.dao.business.mapper.UserBalanceLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;

@Service
public class UserBalanceLogService {

    @Autowired
    UserBalanceLogMapper userBalanceLogMapper;


    public TableDataInfo<UserBalanceLog> queryPageList(UserBalanceLogQuery query, PageQuery pageQuery) {
        return new TableDataInfo(
                userBalanceLogMapper.paginationSelectList(query, pageQuery),
                userBalanceLogMapper.paginationSelectCount(query)
        );
    }


    public UserBalanceLog queryById(Long id) {
        return userBalanceLogMapper.selectById(id);
    }


    @Transactional
    public UserBalanceLog insert(UserBalanceLog entity) {
        userBalanceLogMapper.insert(entity);
        return entity;
    }

    @Transactional
    public void updateById(UserBalanceLog update) {

        UserBalanceLog entity = userBalanceLogMapper.selectById(update.getId());
        if (entity == null) {
            throw new ServiceException();
        }
        entity.setUserId(update.getUserId());
        entity.setRemark(update.getRemark());
        entity.setDiamondBefore(update.getDiamondBefore());
        entity.setDiamondAfter(update.getDiamondAfter());
        entity.setDiamondDelta(update.getDiamondDelta());
        entity.setStarBefore(update.getStarBefore());
        entity.setStarAfter(update.getStarAfter());
        entity.setStarDelta(update.getStarDelta());
        entity.setUpdateTime(LocalDateTime.now());
        userBalanceLogMapper.updateById(entity);
    }


    @Transactional
    public void deleteById(Long id) {
        userBalanceLogMapper.deleteById(id);
    }

    @Transactional
    public void deleteByIds(Long[] ids) {
        userBalanceLogMapper.deleteByIds(Arrays.asList(ids));
    }
}
